package org.dromara.system.api;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxk.service.core.page.PageDataInfo;
import com.lxk.service.core.param.BasePageParam;
import com.lxk.service.tenant.api.TenantServiceApi;
import com.lxk.service.tenant.response.TenantDto;
import lombok.AllArgsConstructor;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.system.domain.vo.SysTenantVo;
import org.dromara.system.service.ISysTenantService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 租户serviceApi
 *
 * @author huahua
 * 2024/8/30
 */
@Service
@AllArgsConstructor
public class TenantServiceApiImpl implements TenantServiceApi {

    private final ISysTenantService sysTenantService;

    /**
     * 租户分页查询
     *
     * @return
     */
    @Override
    public PageDataInfo<TenantDto> page(BasePageParam param) {
        PageQuery query = new PageQuery();
        query.setPageNum(param.getPageNum());
        query.setPageSize(param.getPageSize());
        Page<SysTenantVo> page = sysTenantService.queryPageList(query);
        List<TenantDto> list = new ArrayList<>();
        for (SysTenantVo sysTenantVo : page.getRecords()) {
            TenantDto tenantDto = BeanUtil.copyProperties(sysTenantVo, TenantDto.class);
            list.add(tenantDto);
        }

        PageDataInfo pageDataInfo = new PageDataInfo<>();
        pageDataInfo.setCurrent(page.getCurrent());
        pageDataInfo.setHasNext(page.hasNext());
        pageDataInfo.setSize(page.getSize());
        pageDataInfo.setTotal(page.getTotal());
        pageDataInfo.setRows(list);
        return pageDataInfo;
    }
}
